import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../sugar.dart';

class SugarInit extends StatefulWidget {
  /// 子组件
  final Widget child;

  /// 是否固定字体大小不受系统改变
  final bool isFixedSize;

  /// 是否强制竖屏
  /// app不受陀螺仪的影响
  final bool isForceVerticalScreen;

  /// 是否透明状态栏
  final bool isTransparentStatusBar;

  SugarInit({
    @required this.child,
    this.isFixedSize = true,
    this.isForceVerticalScreen = true,
    this.isTransparentStatusBar = true,
  });

  @override
  _SugarInitState createState() => _SugarInitState();
}

class _SugarInitState extends State<SugarInit> {
  @override
  void initState() {
    super.initState();

    /// 强制竖屏
    if (widget.isForceVerticalScreen) {
      SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
    }

    /// 透明 沉浸式状态栏
    if (widget.isTransparentStatusBar) {
      final _style = SystemUiOverlayStyle(statusBarColor: Colors.transparent);
      SystemChrome.setSystemUIOverlayStyle(_style);
    }
  }

  @override
  Widget build(BuildContext context) {
    final mq = MediaQuery.of(context);
    Sugar.setMediaQuery = mq;

    // 设置文字大小不随系统设置改变
    if (widget.isFixedSize) {
      return MediaQuery(
        data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
        child: widget.child,
      );
    }

    return widget.child;
  }
}
